#include "BezierCurve.h"

template<class V>
BezierCurve<V>::BezierCurve(unsigned int degree)
{
	m_nodes.reserve(degree);
}

template<class V>
void BezierCurve<V>::addNode(const V &node)
{
	// todo: Assert if causing node list to grow
	//if(m_nodes.size()==m_nodes.capacity())
	
	m_nodes.push_back(node);
}

template<class V>
V BezierCurve<V>::pointOnCurve(float t) const
{
	NodeList nodeList = m_nodes;
	V result;
	
	pointOnCurveInternal(nodeList, t, result);

	return result;
}

template<class V>
void BezierCurve<V>::pointOnCurveInternal(NodeList &nodeList, float t, V &result) const
{
	if(nodeList.size()>2)
	{
		for(unsigned int i = 0; i < nodeList.size()-1; ++i)
		{
			nodeList[i] = nodeList[i]*(1-t) + nodeList[i+1]*t;
		}

		nodeList.pop_back();

		pointOnCurveInternal(nodeList, t, result);
	}
	else
	{
		result = nodeList[0]*(1-t) + nodeList[1]*t;
	}
}

// define valid templates classes
template BezierCurve<JPVec2>;
template BezierCurve<JPVec3>;